/*
 * jxUtils Project (2013, 2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxUtils.file.type;

import java.util.ArrayList;
import java.util.List;

import org.jxUtils.check.ParameterCheck;

/**
 * @author Denilson Edinaldo Pinto
 * @project jxUtils
 * @package org.jxUtils.file.type
 * @date 06/02/2013 - 22:25:42
 */
public abstract class FileType implements FileTypeFace {
	
	private String description = "";
	
	private List<String> extensions = null;
	
	private String name = "";
	
	/**
	 * @param name
	 * @param description
	 * @param extensions
	 */
	public FileType(final String name, final String description, final String[] extensions) {
	
		this(name, extensions);
		
		ParameterCheck.isNullOrAbsoluteEmpty(description, "description");
		
		this.description = description;
	}
	
	/**
	 * @param name
	 * @param extensions
	 */
	public FileType(final String name, final String[] extensions) {
	
		this(extensions);
		
		ParameterCheck.isNullOrAbsoluteEmpty(name, "name");
		
		this.name = name;
	}
	
	/**
	 * @param extensions
	 */
	public FileType(final String[] extensions) {
	
		ParameterCheck.isNullOrAbsoluteEmpty(extensions, "extensions");
		
		this.extensions = new ArrayList<String>();
		
		//
		for(final String extention : extensions) {
			
			this.extensions.add(extention);
		}
	}
	
	/**
	 * @param extension
	 */
	@Override
	public final boolean contains(final String extension) {
	
		return contains(extension, extensions);
	}
	
	/**
	 * @return the description
	 */
	@Override
	public final String getDescription() {
	
		return description;
	}
	
	/**
	 * @return the extensions
	 */
	@Override
	public final List<String> getExtensions() {
	
		return extensions;
	}
	
	/**
	 * @return the name
	 */
	@Override
	public final String getName() {
	
		return name;
	}
	
	/**
	 * @param description
	 *        the description to set
	 */
	@Override
	public final void setDescription(final String description) {
	
		this.description = description;
	}
	
	/**
	 * @param extensions
	 *        the extensions to set
	 */
	@Override
	public final void setExtensions(final List<String> extensions) {
	
		this.extensions = extensions;
	}
	
	/**
	 * @param name
	 *        the name to set
	 */
	@Override
	public final void setName(final String name) {
	
		this.name = name;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
	
		return extensions.toString();
	}
	
	/**
	 * @param extension
	 * @param extensions
	 */
	public static final boolean contains(final String extension, final List<String> extensions) {
	
		final String ext = formatExtension(extension);
		
		ParameterCheck.isNullOrAbsoluteEmpty(extensions, "extensions");
		
		//
		for(final String extention : extensions) {
			
			//
			if(extention.equalsIgnoreCase(ext)) {
				
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * @param extension
	 * @param types
	 */
	public static final boolean containsInAny(final String extension, final List<FileTypeFace> types) {
	
		ParameterCheck.isNullOrEmpty(types, "types");
		
		//
		for(final FileTypeFace type : types) {
			
			//
			if(type.contains(extension)) {
				
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * @param extension
	 * @return .extension OR NULL case !isExtension(extension)
	 */
	public static final String formatExtension(final String extension) {
	
		//
		if(isExtension(extension)) {
			
			return extension.startsWith(".") ? extension : "." + extension;
		}
		
		return null;
	}
	
	/**
	 * @param types
	 */
	public static final List<String> getAllExtensions(final List<FileTypeFace> types) {
	
		ParameterCheck.isNullOrEmpty(types, "types");
		
		final List<String> allExtensions = new ArrayList<String>();
		
		//
		for(final FileTypeFace type : types) {
			
			allExtensions.addAll(type.getExtensions());
		}
		
		return allExtensions;
	}
	
	/**
	 * @param extension
	 * @return extension.matches("[.]{0,1}[a-zA-Z0-9_]+")
	 */
	public static final boolean isExtension(final String extension) {
	
		ParameterCheck.isNullOrAbsoluteEmpty(extension, "extension");
		
		return extension.matches("[\\.]{0,1}[a-zA-Z0-9_]+");
	}
}
